import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
import {useUserStore} from "@/store/module/user";

const routes: RouteRecordRaw[] = [
    {
        name: '/',
        path: '/',
        redirect: '/home',
        children: [
            {
                name: 'home',
                path: 'home',
                component: () => import('@/views/home/index.vue')
            }
        ]
    },
    {
        name: 'login',
        path: '/login',
        component: () => import('@/views/user/login.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes: routes
})

router.beforeEach((to, from, next) => {
    if (to.path !== '/login' && useUserStore().token === '') {
        next('/login')
    } else {
        next()
    }
})

export default router
